package com.zz.springboot.util;


import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ChineseNumberConverter {

    private static final Map<Character, Integer> CHINESE_NUMBERS = new HashMap<>();

    static {
        CHINESE_NUMBERS.put('零', 0);
        CHINESE_NUMBERS.put('一', 1);
        CHINESE_NUMBERS.put('二', 2);
        CHINESE_NUMBERS.put('三', 3);
        CHINESE_NUMBERS.put('四', 4);
        CHINESE_NUMBERS.put('五', 5);
        CHINESE_NUMBERS.put('六', 6);
        CHINESE_NUMBERS.put('七', 7);
        CHINESE_NUMBERS.put('八', 8);
        CHINESE_NUMBERS.put('九', 9);
        CHINESE_NUMBERS.put('十', 10);
        CHINESE_NUMBERS.put('百', 100);
        CHINESE_NUMBERS.put('千', 1000);
        CHINESE_NUMBERS.put('万', 10000);
    }

    /**
     * 将字符串中的汉字数字转换为阿拉伯数字
     * @param text 包含汉字数字的字符串
     * @return 转换后的字符串
     */
    public static String convertChineseNumbersToArabic(String text) {
        // 使用正则表达式匹配中文数字（包括万、千、百、十）
        Pattern pattern = Pattern.compile("[零一二三四五六七八九万千百十]+");
        Matcher matcher = pattern.matcher(text);
        StringBuffer result = new StringBuffer();

        while (matcher.find()) {
            String chineseNum = matcher.group();
            if (!chineseNum.isEmpty()) {
                try {
                    long arabicNum = convertComplexChineseNumber(chineseNum);
                    matcher.appendReplacement(result, String.valueOf(arabicNum));
                } catch (NumberFormatException e) {
                    // 如果转换失败，则保留原字符串
                }
            }
        }
        matcher.appendTail(result);

        return result.toString();
    }

    /**
     * 转换复杂的汉字数字为阿拉伯数字
     * @param chineseNumber 汉字数字字符串
     * @return 对应的阿拉伯数字
     */
    public static long convertComplexChineseNumber(String chineseNumber) {
        if (chineseNumber.isEmpty()) {
            return 0;
        }

        // 处理特殊情况：只有"十"
        if (chineseNumber.equals("十")) {
            return 10;
        }

        long result = 0;
        long wanPart = 0; // 万位部分
        long lowerPart = 0; // 万以下部分
        long currentNumber = 0;

        for (int i = 0; i < chineseNumber.length(); i++) {
            char ch = chineseNumber.charAt(i);
            Integer value = CHINESE_NUMBERS.get(ch);

            if (value == null) {
                throw new NumberFormatException("Invalid character: " + ch);
            }

            if (ch == '万') {
                // 遇到万字，前面的部分乘以10000
                wanPart = (wanPart + currentNumber) * 10000;
                currentNumber = 0;
                lowerPart = 0; // 重置低位部分
            } else if (value < 10) {
                // 处理数字字符（0-9）
                currentNumber = value;

                // 如果是最后一个字符，需要加上这个数字
                if (i == chineseNumber.length() - 1) {
                    lowerPart += currentNumber;
                }
            } else {
                // 处理单位字符（十、百、千）
                if (ch == '千') {
                    lowerPart += currentNumber * 1000;
                    currentNumber = 0;
                } else if (ch == '百') {
                    lowerPart += currentNumber * 100;
                    currentNumber = 0;
                } else if (ch == '十') {
                    // 特殊处理"十几"的情况
                    if (i == 0) {
                        // 以"十"开头，如"十五"，相当于"一十五"
                        lowerPart += 1 * 10;
                    } else {
                        lowerPart += currentNumber * 10;
                        currentNumber = 0;
                    }
                }
            }
        }

        return wanPart + lowerPart;
    }
}